﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace GDI_AnalysisComponents
{
    //单元格类

    [Serializable()]
    public class GUnit
    {
        private static int count = 0;
        private int id;
        private string name;
        private string type;
        private float width;
        private float hight;
        private float x;
        private float y;
        private PointF location;
        private int fartherGUnitID;
        private int gcomponentID;

        public int ID
        {
            get { return id; }
        }

        public  int Count
        {
            get { return count; }
            set { count = value; }
        }


        public int GComponentId
        {
            get { return gcomponentID; }
            set { gcomponentID = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Type
        {
            get { return type; }
            set { type = value; }
        }

        public float X
        {
            get { return x; }
            set { x = value; }
        }

        public float Y
        {
            get { return y; }
            set { y = value; }
        }

        public PointF Location
        {
            get { return new PointF(x,y); }
            set { location = value; }
        }

        public float Width
        {
            get { return width; }
            set { width = value; }
        }

        public float Hight
        {
            get { return hight; }
            set { hight = value; }
        }

        public int FartherGUnitID
        {
            get { return fartherGUnitID; }
            set { fartherGUnitID = value; }
        }

        //构造函数
        public GUnit(string name, string type)
        {
            count++;
            this.id = count;
            this.name = name;
            this.type = type;
            this.width = 50;
            this.Hight = 20;
            this.x = 0;
            this.y = 0;
            this.location=new PointF(0,0);
            this.fartherGUnitID = -1;
            this.gcomponentID = -1;
        }

        //用以生成组件头
        public GUnit(string name)
        {
            this.id = 0;
            this.name = name;
            this.type = "";
            this.width = 50;
            this.Hight = 20;
            this.x = 0;
            this.y = 0;
            this.location = new PointF(0, 0);
            this.fartherGUnitID = -1;
            this.gcomponentID = -1;
        }

        //绘制单元
        public void DrawGunit(Graphics g)
        {
            PointF pt = new PointF(x, y);
            string text;
            Font font;
            RectangleF rectF = new RectangleF(x, y, width, hight);
            Rectangle rect = new Rectangle((int)x, (int)y, (int)width, (int)hight);
            StringFormat strFormat = new StringFormat();

            strFormat.Alignment = StringAlignment.Center;
            strFormat.LineAlignment = StringAlignment.Center;
            //strFormat.Trimming = StringTrimming.Character;
            strFormat.Trimming = StringTrimming.EllipsisCharacter;

            Pen pen = new Pen(Color.Black);
            SolidBrush textSBrush = new SolidBrush(Color.Black);
            SolidBrush FillSBrush = new SolidBrush(Color.White);

            if (type != null)
            {
                strFormat.Alignment = StringAlignment.Near;
                text ="-"+ name + ":" + type;
                font= new Font("Verdana", 10, FontStyle.Italic);
                textSBrush = new SolidBrush(Color.Black);
                FillSBrush = new SolidBrush(Color.White);
            }
            else
            {
                //strFormat.Alignment = StringAlignment.Near;
                text = name ;
                font = new Font("Verdana", 10, FontStyle.Bold);
                textSBrush = new SolidBrush(Color.Black);
                FillSBrush=new SolidBrush(Color.White);
            }
            g.FillRectangle(FillSBrush, rect);
            g.DrawRectangle(pen,rect);
            g.DrawString(text, font, textSBrush, rectF, strFormat);
            font.Dispose();        
            
        }

        //获取左边中点
        public PointF GetLeftMidPoint()
        {
            return new PointF(x, y + hight / 2);
        }

        //获取右边中点
        public PointF GetRightMidPoint()
        {
            return new PointF(x+width, y + hight / 2);
        }

        //获取单元矩形
        public RectangleF getGUnitRect()
        {
            return new RectangleF(x, y, width, hight);
        }
    }
}
